//
//  BGFaBuEntrustController.m
//  bigMall
//
//  Created by ios on 16/9/22.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGFaBuEntrustController.h"
#import "BGBasicUrl.h"
#import "UIImageView+WebCache.h"
#import "SZAddImage.h"
#import "BGDateResult.h"
#import "BGHttpTool.h"
#import "BGHomeAddSadModel.h"

@interface BGFaBuEntrustController ()<UIScrollViewDelegate,UIPickerViewDelegate,UIPickerViewDataSource> {
    
    UIScrollView *_scrolleView;
    UITextField *phoneFiled;//手机号
    UITextField *imageFiled;//小图片
    UIImageView *imageView;
    UITextField *messageFiled;//信息的
    UITextField *titleFiled;//委托的标题
    UITextField *contentFiled;//委托的内容
    UITextField *typeFiled;//委托的类型
    UIView *wancheng;//完成的按钮
    NSArray *_dataArr;
    NSMutableString *strImageId;
    
}
@property (nonatomic,strong) UIPickerView *picker;
@end

@implementation BGFaBuEntrustController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"委托业务";
    // Do any additional setup after loading the view.
    [self createUI];
    [self imageClick];
    _dataArr = @[@"出售",@"采购",@"委托进口",@"全委托"];
    
    self.imageData = [NSMutableArray array];
    
}
- (void)createUI {
    
    //滚动的视图
    _scrolleView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, BGWidth, BGHight)];
    _scrolleView.backgroundColor = [UIColor whiteColor];
    _scrolleView.showsVerticalScrollIndicator = NO;
    _scrolleView.scrollEnabled = YES;
    _scrolleView.delegate = self;
    _scrolleView.userInteractionEnabled = YES;
    [self.view addSubview:_scrolleView];
    
    for (int i = 1; i <= 5; i++) {
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(21, (i - 1) * 64, self.view.bounds.size.width - 42, 1)];
        
        if (i == 1) {
            
            view.hidden = YES;
            
        }
        
        view.backgroundColor = dockwhite_;
        
        [_scrolleView addSubview:view];
        
    }
    
    //手机号
    UILabel *pnoneLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 64 / 3 + 64 - 64, BGWidth / 5, 64 / 3)];
    
    pnoneLab.text = @"手机号:";
    pnoneLab.font = [UIFont systemFontOfSize:15];
    pnoneLab.textAlignment = NSTextAlignmentLeft;
    [_scrolleView addSubview:pnoneLab];
    
    phoneFiled = [[UITextField alloc] initWithFrame:CGRectMake(pnoneLab.right, pnoneLab.y, 140, pnoneLab.height)];
    
    phoneFiled.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    phoneFiled.placeholder = @"请填写手机号";
    
    [_scrolleView addSubview:phoneFiled];
    
    //iamge
    UILabel *iamgeLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 64 / 3 + 64 - 64 + 64 * 1, BGWidth / 5, 64 / 3)];
    
    iamgeLab.text = @"验证码:";
    iamgeLab.font = [UIFont systemFontOfSize:15];
    iamgeLab.textAlignment = NSTextAlignmentLeft;
    [_scrolleView addSubview:iamgeLab];
    
    imageFiled = [[UITextField alloc] initWithFrame:CGRectMake(iamgeLab.right, iamgeLab.y, BGWidth / 2.5, iamgeLab.height)];
    
    imageFiled.placeholder = @"请填写图形证码";
    
    [_scrolleView addSubview:imageFiled];
    
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageFiled.right, iamgeLab.y - 15, BGWidth - 21 - imageFiled.right, iamgeLab.height + 30)];
    
    imageView.contentMode = UIViewContentModeScaleToFill;
    
    imageView.userInteractionEnabled = YES;
    
    [_scrolleView addSubview:imageView];
    
    //添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    
    [tap addTarget:self action:@selector(imageClick)];
    
    [self imageClick];
    
    [imageView addGestureRecognizer:tap];
    
    //发布的Btn
    UIButton *xiugaiBtn = [[UIButton alloc] initWithFrame:CGRectMake(21, BGHight - 60, self.view.bounds.size.width - 42, 50)];
    
    xiugaiBtn.clipsToBounds = YES;
    
    xiugaiBtn.layer.cornerRadius = 10;
    
    [xiugaiBtn setTitle:@"发布委托" forState:UIControlStateNormal];
    
    [xiugaiBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    xiugaiBtn.backgroundColor = [UIColor redColor];
    
    [xiugaiBtn addTarget:self action:@selector(fabu) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:xiugaiBtn];
    
    //title
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 64 / 3 + 64 - 64 + 64 * 2, BGWidth / 4.6, 64 / 3)];
    titleLab.text = @"委托标题:";
    titleLab.font = [UIFont systemFontOfSize:15];
    titleLab.textAlignment = NSTextAlignmentLeft;
    [_scrolleView addSubview:titleLab];
    
    titleFiled = [[UITextField alloc] initWithFrame:CGRectMake(titleLab.right, titleLab.y, BGWidth - 42 - titleLab.right, titleLab.height)];
    titleFiled.placeholder = @"请填写委托标题，5～200个字符";
    
    [_scrolleView addSubview:titleFiled];
    
    //content
    UILabel *contentLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 64 / 3 + 64 - 64 + 64 * 3, BGWidth / 4.6, 64 / 3)];
    contentLab.text = @"委托内容:";
    contentLab.font = [UIFont systemFontOfSize:15];
    contentLab.textAlignment = NSTextAlignmentLeft;
    [_scrolleView addSubview:contentLab];
    
    contentFiled = [[UITextField alloc] initWithFrame:CGRectMake(contentLab.right, contentLab.y, BGWidth - 42 - contentLab.right, contentLab.height)];
    contentFiled.placeholder = @"请填写委托内容，5～200个字符";
    
    [_scrolleView addSubview:contentFiled];
    
    //type
    UILabel *typeLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 64 / 3 + 64 - 64 + 64 * 4, BGWidth / 4.6, 64 / 3)];
    typeLab.text = @"委托类型:";
    typeLab.font = [UIFont systemFontOfSize:15];
    typeLab.textAlignment = NSTextAlignmentLeft;
    [_scrolleView addSubview:typeLab];
    
    typeFiled = [[UITextField alloc] initWithFrame:CGRectMake(typeLab.right, typeLab.y, 100, typeLab.height)];
    typeFiled.placeholder = @"请选择";
    typeFiled.userInteractionEnabled = NO;
    [_scrolleView addSubview:typeFiled];
    
    UIImageView *headImage  = [[UIImageView alloc] initWithFrame:CGRectMake(BGWidth - 21, typeLab.y + 3, 10, 10)];
    headImage.image = [UIImage imageNamed:@"更多信息"];
    headImage.contentMode = UIViewContentModeScaleToFill;
    [_scrolleView addSubview:headImage];
    
    UIButton *buttona = [[UIButton alloc] initWithFrame:CGRectMake(typeLab.x, typeLab.y, BGWidth - 21, typeLab.height)];
    [buttona addTarget:self action:@selector(pickera) forControlEvents:UIControlEventTouchUpInside];
    [_scrolleView addSubview:buttona];
    
    //修饰图
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 4 * 64 + 64, self.view.bounds.size.width, 10)];
    v.backgroundColor = dockwhite_;
    [_scrolleView addSubview:v];
    
    //上传凭证的view
    UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(0, v.bottom + 64, BGWidth, 1)];
    v1.backgroundColor = dockwhite_;
    [_scrolleView addSubview:v1];
    
    UILabel *pingzhengLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 64 / 3 + 64 - 64 + 64 * 5 + 9, BGWidth / 4.6, 64 / 3)];
    pingzhengLab.text = @"上传凭证:";
    pingzhengLab.font = [UIFont systemFontOfSize:15];
    pingzhengLab.textAlignment = NSTextAlignmentLeft;
    [_scrolleView addSubview:pingzhengLab];
    
    //imagePicker
    SZAddImage *imagePicker = [[SZAddImage alloc] initWithFrame:CGRectMake(0, v1.bottom, BGWidth, BGHight / 2)];
    [_scrolleView addSubview:imagePicker];
    _scrolleView.contentSize = CGSizeMake(BGWidth, imagePicker.bottom);
    
    //picker
    self.picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, [UIApplication sharedApplication].keyWindow.bottom - [UIScreen mainScreen].bounds.size.height / 3, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 3)];
    
    self.picker.delegate = self;
    self.picker.dataSource = self;
    self.picker.backgroundColor = [UIColor whiteColor];
    self.picker.hidden = YES;
    self.picker.tag = 1;
    [self.picker selectRow:0 inComponent:0 animated:YES];
    [self.view addSubview:self.picker];
    [self.picker showsSelectionIndicator];
    
    [self.picker reloadAllComponents];
    //创建确定的按钮
    wancheng = [[UIView alloc] initWithFrame:CGRectMake(0, self.picker.y - 30, [UIScreen mainScreen].bounds.size.width, 30)];
    wancheng.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:wancheng];
    
    UIView *abc = [[UIView alloc] initWithFrame:CGRectMake(0, 29, [UIScreen mainScreen].bounds.size.width, 1)];
    abc.backgroundColor = [UIColor lightGrayColor];
    [wancheng addSubview:abc];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(wancheng.width - [UIScreen mainScreen].bounds.size.width / 4.3, 0, [UIScreen mainScreen].bounds.size.width / 4.3, 30)];
    [btn setTitle:@"完成" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    btn .titleLabel.textAlignment = NSTextAlignmentRight;
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    wancheng.hidden = YES;
    [wancheng addSubview:btn];
    
    
}

- (void)pickera {
    
    typeFiled.text = _dataArr[0];
    wancheng.hidden = NO;
    self.picker.hidden = NO;
    
}
- (void)btnClick {
    
    wancheng.hidden = YES;
    self.picker.hidden = YES;
    
}
- (void)fabu {
    
     NSLog(@"imageData === %@",[BGFaBuEntrustController share].imageData);
    
    for (int i = 0; i < [BGFaBuEntrustController share].imageData.count; i++) {
        
        if (i == 0) {
            strImageId = [NSMutableString stringWithFormat:@"%@",[BGFaBuEntrustController share].imageData[i]];
        }else {
            strImageId = [NSMutableString stringWithFormat:@"%@,%@",strImageId,[BGFaBuEntrustController share].imageData[i]];
        }
        
    }
    
    if ([BGFaBuEntrustController share].imageData.count == 0) {
        strImageId = [NSMutableString stringWithString:@""];
    }
    
    NSLog(@"strImageId === %@",strImageId);
    
    if ([titleFiled.text  isEqual: @""] || [contentFiled.text  isEqual: @""] || [phoneFiled.text  isEqual: @""] || [imageFiled.text  isEqual: @""] || [typeFiled.text  isEqual: @""]) {
        [self showMessage:@"信息填写的不全!"];
        return;
    }
    
    if ([typeFiled.text isEqualToString:@"出售"]) {
        self.typeStr = @"sell";
    }else if ([typeFiled.text isEqualToString:@"采购"]){
        self.typeStr = @"buy";
    }else if ([typeFiled.text isEqualToString:@"委托进口"]){
        self.typeStr = @"imports";
    }else if ([typeFiled.text isEqualToString:@"全委托"]){
        self.typeStr = @"allsad";
    }
    
        [BGDateResult addSadWithParamDic:@{@"name":titleFiled.text,
                                           @"cont":contentFiled.text,
                                           @"phone":phoneFiled.text,
                                           @"type":self.typeStr,
                                           @"profilePic":strImageId,
                                           @"imgCode":imageFiled.text,
                                           @"imgType":@"addSad",
                                           @"imgTypeId":@"1"}
                                 success:^(NSObject *result) {
                         
                                     BGHomeAddSadModel *model = (BGHomeAddSadModel *)result;
                                     
                                     if ([model.status isEqualToString:@
                                         "1"]) {
                                        
                                         [self showMessage:model.msg];
                                         
                                     }else {
                                         [self showMessage:model.msg];
                                     }
                                     
            
        } failure:^(NSError *erreo) {
            
        }];
    
}
- (void)imageClick {
    
    NSString *url = [BGBasicUrl getBasicUrl];
    
    [BGHttpTool POSTImage:[NSString stringWithFormat:@"%@/index.php?s=/bigmall/tools/imgvc/type/addSad/id/1.html",url] paramenters:nil progress:^(NSProgress *progress) {
        
    } success:^(id responseObject) {
        
        imageView.image = [UIImage imageWithData:responseObject];
        
    } failure:^(NSError *error) {
        
    }];
    
    
}
- (void)showMessage:(NSString *)message {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        if (![message isEqualToString:@"信息填写的不全!"]) {
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }
  
    }];
    

    
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}


#pragma mark pickerDatasourece
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    return 1;
    
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    return _dataArr.count;
    
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    return _dataArr[row];
    
    
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        // Setup label properties - frame, font, colors etc
        //adjustsFontSizeToFitWidth property to YES
        pickerLabel.minimumFontSize = 8;
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:18]];
    }
    // Fill the label text here
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    typeFiled.text = _dataArr[row];
    
}
+ (BGFaBuEntrustController *)share {
    
    static BGFaBuEntrustController *a = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate,^{
        a = [[self alloc] init];
    });
    return a;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    for (UIView *view in scrollView.subviews) {
        if ([view isKindOfClass:[NSClassFromString(@"UITextField") class]]) {
            UITextField *t = (UITextField *)view;
            [t resignFirstResponder];
        }
    }
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    for (UIView *view in _scrolleView.subviews) {
        if ([view isKindOfClass:[NSClassFromString(@"UITextField") class]]) {
            UITextField *t = (UITextField *)view;
            [t resignFirstResponder];
        }
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
